<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="账单所属月份" prop="billMonth">
        <el-date-picker clearable v-model="queryParams.billMonth" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择账单所属月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="请选择支付状态" clearable>
          <el-option v-for="dict in dict.type.zk_rent_bill_payment_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-input v-model="queryParams.paymentMethod" placeholder="请输入支付方式" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="实际支付时间" prop="paymentTime">
        <el-date-picker clearable v-model="queryParams.paymentTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择实际支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['rent:bill:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['rent:bill:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['rent:bill:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['rent:bill:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租金ID" align="center" prop="id" width="80" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="房东" align="center" prop="landlordName" />
      <el-table-column label="租客" align="center" prop="tenantName" />
      <el-table-column label="房源" align="center" prop="propertyName" />
      <el-table-column label="账单月份" align="center" prop="billMonth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billMonth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应缴租金" align="center" prop="rentAmount" />
      <el-table-column label="实缴金额" align="center" prop="paidAmount" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zk_rent_bill_payment_status" :value="scope.row.paymentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['rent:bill:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['rent:bill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改租金账单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="租客" prop="tenantId">
              <el-select v-model="form.tenantId" placeholder="请选择租客">
                <el-option v-for="tenant in listTenant" :key="tenant.id" :label="tenant.name"
                  :value="tenant.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房源" prop="propertyId">
              <el-select v-model="form.propertyId" placeholder="请选择房源">
                <el-option v-for="property in listProperty" :key="property.id" :label="property.propertyName"
                  :value="property.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同" prop="contractId">
              <el-select v-model="form.contractId" placeholder="请选择合同">
                <el-option v-for="contrat in listContract" :key="contrat.id" :label="contrat.contractNo"
                  :value="contrat.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单所属月份" prop="billMonth">
              <el-date-picker clearable v-model="form.billMonth" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择账单所属月份">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应缴租金" prop="rentAmount">
              <el-input v-model="form.rentAmount" placeholder="请输入应缴租金" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实缴金额" prop="paidAmount">
              <el-input v-model="form.paidAmount" placeholder="请输入实缴金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应缴日期" prop="dueDate">
              <el-date-picker clearable v-model="form.dueDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择应缴日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际支付时间" prop="paymentTime">
              <el-date-picker clearable v-model="form.paymentTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择实际支付时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="支付状态" prop="paymentStatus">
          <el-radio-group v-model="form.paymentStatus">
            <el-radio v-for="dict in dict.type.zk_rent_bill_payment_status" :key="dict.value"
              :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>

        <el-form-item label="缴费备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listAllProperty,
  } from "@/api/rent/property"

  import {
    listAllTenant,
  } from "@/api/rent/tenant"

  import {
    listAllContract,
  } from "@/api/rent/contract"

  import {
    listBill,
    getBill,
    delBill,
    addBill,
    updateBill
  } from "@/api/rent/bill"

  export default {
    name: "Bill",
    dicts: ['zk_rent_bill_payment_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 租金账单表格数据
        billList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 缴费备注时间范围
        daterangeCreateTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contractId: null,
          landlordId: null,
          tenantId: null,
          propertyId: null,
          billMonth: null,
          rentAmount: null,
          paidAmount: null,
          dueDate: null,
          paymentStatus: null,
          paymentMethod: null,
          paymentTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          contractId: [{
            required: true,
            message: "对应合同ID不能为空",
            trigger: "blur"
          }],
          landlordId: [{
            required: true,
            message: "房东ID不能为空",
            trigger: "blur"
          }],
          tenantId: [{
            required: true,
            message: "租客ID不能为空",
            trigger: "blur"
          }],
          propertyId: [{
            required: true,
            message: "房源ID不能为空",
            trigger: "blur"
          }],
          billMonth: [{
            required: true,
            message: "账单所属月份不能为空",
            trigger: "blur"
          }],
          rentAmount: [{
            required: true,
            message: "应缴租金不能为空",
            trigger: "blur"
          }],
          dueDate: [{
            required: true,
            message: "应缴日期不能为空",
            trigger: "blur"
          }],
        },
        listProperty: [], //房源信息
        listTenant: [], //租客信息
        listContract: [], //房源信息
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        //加载房源列表
        listAllProperty({}).then(response => {
          this.listProperty = response.rows
        })
        //加载租客列表
        listAllTenant({}).then(response => {
          this.listTenant = response.rows
        })
        //加载租客列表
        listAllContract({}).then(response => {
          this.listContract = response.rows
        })
        //加载合同列表
        this.getList()
      },
      /** 查询租金账单列表 */
      getList() {
        this.loading = true
        this.queryParams.params = {}
        if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
          this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
          this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
        }
        listBill(this.queryParams).then(response => {
          this.billList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          contractId: null,
          landlordId: null,
          tenantId: null,
          propertyId: null,
          billMonth: null,
          rentAmount: null,
          paidAmount: null,
          dueDate: null,
          paymentStatus: null,
          paymentMethod: null,
          paymentTime: null,
          remark: null,
          createTime: null,
          updateTime: null
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeCreateTime = []
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加租金账单"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getBill(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改租金账单"
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateBill(this.form).then(response => {
                this.$modal.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addBill(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除租金账单编号为"' + ids + '"的数据项？').then(function() {
          return delBill(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('rent/bill/export', {
          ...this.queryParams
        }, `bill_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>
